// 商城会员信息
import { getBrandIdCookie, getUserTokenCookie } from '@biztools/info';
import logger from '@common/logger';
import { getMemberInfoNew, getMemberShopCard } from '@common/market';
import { MEMBER_BIZ_TYPE } from '@constants/member';
import { GET_SHOP_MEMBER_INFO, SET_SHOP_MEMBER_CARD } from '../mutationType';
const shopMemberInfo = {
    state: {
        memberInfo: null, // 会员信息
        memberCard: null,
    },
    mutations: {
        GET_SHOP_MEMBER_INFO: (state, memberInfo) => {
            Object.assign(state, { memberInfo });
        },
        SET_SHOP_MEMBER_CARD: (state, memberCard) => {
            Object.assign(state, { memberCard });
        },
    },
    actions: {
        getShopMemberInfo: async ({ commit, state }) => {
            const info = await getMemberInfoNew(getUserTokenCookie(), MEMBER_BIZ_TYPE.shop);
            logger.success('ShopMember => 获取的用户信息为', info);
            commit(GET_SHOP_MEMBER_INFO, info);
            if (info.groups.length > 0) {
                commit(SET_SHOP_MEMBER_CARD, info.groups[0]);
            }
        },
        getShopCard: async ({ dispatch, state }) => {
            if (state.memberCard) return;
            logger.debugger('ShopMember => 执行会员领卡', getUserTokenCookie(), getBrandIdCookie());
            // 执行领卡操作，领取商城的卡
            await getMemberShopCard(getUserTokenCookie(), getBrandIdCookie());
            // 更新会员信息
            dispatch('getShopMemberInfo');
        },
    },
};

export default shopMemberInfo;
